package com.hzw.saas.web.app.product.controller;


import java.util.Objects;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hzw.saas.api.product.IBalanceOrderService;
import com.hzw.saas.api.product.IItemOrderService;
import com.hzw.saas.api.product.dto.BalanceOrderDto;
import com.hzw.saas.api.product.dto.BalanceOrderInfoDto;
import com.hzw.saas.api.product.dto.ItemOrderDto;
import com.hzw.saas.api.product.dto.ItemOrderInfoDto;
import com.hzw.saas.common.config.exception.DefaultSaasException;
import com.hzw.saas.common.config.util.PageParam;
import com.hzw.saas.common.security.pojo.dto.SaasUser;
import com.hzw.saas.common.security.utils.SecurityUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author zzl
 * @since 2021-01-21
 */
@Controller
@RequestMapping("/order")
@RequiredArgsConstructor
@Api(tags = "会员中心/用户订单")
public class OrderController {

    private final IItemOrderService itemOrderService;
    private final IBalanceOrderService balanceOrderService;

    @GetMapping("/item/{orderId}")
    @ApiOperation(value = "获取商品订单详情", notes = "根据订单ID查询商品订单详情")
    @ApiOperationSort(2)
    public ResponseEntity<ItemOrderInfoDto> item(@PathVariable String orderId) {
        SaasUser user = SecurityUtils.getUser();
        ItemOrderInfoDto itemOrderInfoDto = itemOrderService.getInfo(orderId);
        if (!Objects.equals(user.getUserId(), itemOrderInfoDto.getOrder().getUserId())) {
            throw DefaultSaasException.build("请求非法").status(HttpStatus.FORBIDDEN);
        }
        return ResponseEntity.ok(itemOrderInfoDto);
    }

    @GetMapping("/items")
    @ApiOperation(value = "获取商品订单列表", notes = "获取个人商品订单列表,可分页")
    @ApiOperationSort(3)
    public ResponseEntity<IPage<ItemOrderInfoDto>> items(PageParam pageParam) {
        SaasUser user = SecurityUtils.getUser();
        ItemOrderDto cnd = new ItemOrderDto();
        cnd.setUserId(user.getUserId());
        IPage<ItemOrderInfoDto> itemOrderInfoDtoIPage = itemOrderService.listInfo(pageParam, cnd);
        return ResponseEntity.ok(itemOrderInfoDtoIPage);
    }


    @GetMapping("/balance/{orderId}")
    @ApiOperation(value = "获取充值订单详情", notes = "根据订单ID查询充值订单详情")
    @ApiOperationSort(4)
    public ResponseEntity<BalanceOrderInfoDto> balance(@PathVariable String orderId) {
        SaasUser user = SecurityUtils.getUser();
        BalanceOrderInfoDto balanceOrderInfoDto = balanceOrderService.getInfo(orderId);
        if (!Objects.equals(user.getUserId(), balanceOrderInfoDto.getOrder().getOrderId())) {
            throw DefaultSaasException.build("请求非法").status(HttpStatus.FORBIDDEN);
        }
        return ResponseEntity.ok(balanceOrderInfoDto);
    }

    @GetMapping("/balances")
    @ApiOperation(value = "获取充值订单列表", notes = "获取个人充值订单列表,可分页")
    @ApiOperationSort(5)
    public ResponseEntity<IPage<BalanceOrderInfoDto>> balances(PageParam pageParam) {
        SaasUser user = SecurityUtils.getUser();
        BalanceOrderDto cnd = new BalanceOrderDto();
        cnd.setUserId(user.getUserId());
        IPage<BalanceOrderInfoDto> balanceOrderInfoDtoIPage = balanceOrderService.listInfo(pageParam, cnd);
        return ResponseEntity.ok(balanceOrderInfoDtoIPage);
    }


}
